Java final, finally and finalize
π Java Fun: final, finally, and finalize Explained! πβ
Welcome to this exciting Java tutorial where we unravel the mysteries of final
, finally
, and finalize
. Buckle up! π
π 1. Java final
Keywordβ
The final
keyword is like a "No Entry" sign for modifications. It can be applied to variables, methods, and classes, but it behaves differently in each case. Letβs explore! π
πΉ 1.1. final
Variables β Once Set, Forever Unchangedβ
A variable marked final
can be assigned a value only once! Hereβs where you can initialize it:
β At the time of declaration ποΈ
β Inside the constructor π
Code Time! β³β
public class MyClass {
// During declaration
public final String MAJOR_VERSION = "1";
public final String MINOR_VERSION;
public MyClass() {
// In constructor
this.MINOR_VERSION = "2";
}
}
π‘ Fact: Once initialized, trying to change a final
variable will lead to a compilation error π¨:
"The final field MyClass.VERSION cannot be assigned."
πΉ 1.2. final
Methods β No Override Allowed! π«β
A method marked final
is like a parent who says, "No, you can't change the family rules!" π
It cannot be overridden by child classes.
Code Time0! β³β
public class ParentClass {
public final void showMyName() {
System.out.println("In ParentClass");
}
}
β Trying to override this method? Say hello to a compile-time error:
"Cannot override the final method from ParentClass."
πΉ 1.3. final
Classes β The End of Inheritance! ββ
A final
class is off-limits for inheritance. No subclasses allowed! π·
Code Time1! β³β
public final class ParentClass {
public void showMyName() {
System.out.println("In ParentClass");
}
}
π Trying to inherit from this class?
π¨ ERROR: "Cannot inherit from final ParentClass."
π₯ 2. Java finally
Block β Cleanup Crew in Action! π§Ήβ
The finally
block is like that one friend who ALWAYS shows up, no matter what. Whether an exception occurs or not, this block will execute.
π Use Case: Releasing Resourcesβ
β Closing files π
β Closing database connections ποΈ
β Releasing memory π§
Code Time2! β³β
try {
// Open file π
// Read file π
}
catch(Exception e) {
// Handle exception π¨
}
finally {
// Close file π
}
π‘ Fact: The finally
block is like a janitorβit always cleans up before leaving the room! π§½
ποΈ 3. Java finalize()
Method β Garbage Collectorβs Goodbye! πβ
β οΈ Note: Java 18 (JEP-421
) has deprecated finalize()
, and it will be removed in a future release! π±
finalize()
is a special method that the garbage collector calls when an object is about to be trashed! ποΈ
πΉ Defined in Object
Classβ
protected void finalize() throws Throwable
π Key Facts:β
β Used for cleanup before the object is destroyed. π§Ή
β If an exception is thrown in finalize()
, it's ignored. π«
β finalize()
is never called more than once for the same object. πβ
π’ Pro Tip: Instead of finalize()
, use try-with-resources or cleaners for resource management. π
π― 4. Conclusion β The Final Showdown! π¬β
Keyword | Purpose π― | Can Be Used With? π€ | Key Rule π |
---|---|---|---|
final | Restriction π | Variables, Methods, Classes | No changes allowed! β |
finally | Cleanup π§Ή | Try-Catch Blocks | Always executes! π |
finalize() | Cleanup Before GC ποΈ | Objects | Deprecated! π¨ |
π‘ Fun Fact: The only thing final
, finally
, and finalize()
have in common isβ¦ they all start with βfinalβ! π€―
π₯ Thatβs it, folks! Youβre now a pro at final
, finally
, and finalize()
! Keep coding and stay awesome! π»β¨
Happy Learning! ππ